<?php
/**
 * 用户案件状态 
 */
class Cli_CasehuadaoController extends Ctrl_Cli{
	public function indexAction(){
		while(true){
            $tTime = time();
            $tRunlogdir = APPLICATION_PATH . '/shell/runlog/cli_casehuadao.run';
            Tool_Fnc::writefile($tRunlogdir , $tTime);

			$this->opt();
			sleep(5);
		}
		exit;
	}
	private function opt(){
        $tCHMO = new Case_HuadaoModel;
        $tCHDatas = $tCHMO->field('*')->where('status = 2 or status = 4 ')->fList();

        $tUCMO = new User_CaseModel;
        $tCHIMO = new Case_Huadao_IssueModel;
        $tUCLMO = new User_Case_LogModel;
        $tUCFMO = new User_Case_FailModel; 
        $tTime = time();
    

        foreach($tCHDatas as $tRow){
            //扩展影像类型
            $tPIMO = new Policy_InfoModel; 
            $tPIRow = $tPIMO->field('datatype')->where('policyno = \''.$tRow['hd_policyno'].'\'')->fRow();

            if($tRow['status'] == 2){#华道预结案 
                $tData = array(
                    'hd_caseno' => $tRow['hd_caseno'],
                    'loss_notice' => $tRow['loss_notice'],
                    'status' => 3,
                    'payment_amount' => $tRow['payment_amount'],
                    'id' => $tRow['case_id'],
                    'datatype' => $tPIRow['datatype'],
                    'updated' => $tTime,
                    'is_dishonor' => $tRow['is_dishonor'],
                );      
				if($tUCMO->update($tData)){
					//发送短信提醒
					$tSMO = new SmsModel;
					$tUMO = new UserModel;
					$tSql = 'select u.phone,u.critcode from '.$tUCMO->table.' uc left join '.$tUMO->table.' u on uc.uid = u.id where uc.id = '.$tRow['case_id'];
					$tPhone = $tUCMO->query($tSql)[0]['phone'];
					$tCritcode = $tUCMO->query($tSql)[0]['critcode'];
					if(!empty($tPhone)){
						$tHCTGMO = new Huadao_Core_T_GrouppolicyinsuredModel;
						$tSTMO = new Sms_TemplateModel;
						$tPolicyno = $tHCTGMO->field('POLICYNO')->where('INSUREDIDNO = "'.$tCritcode.'"')->fRow()['POLICYNO'];
						$tMid = $tSTMO->field('tpl_id')->where('policyno = "'.$tPolicyno.'" and type = 3')->fRow()['tpl_id'];
						$tSMO->caseStatus($tPhone,$tMid);
					}
					
				};

            /*}elseif($tRow['status'] == 3){#华道问题件
                //更新案件状态
                $tData = array(
                    'status' => 5,    
                    'id' => $tRow['case_id'],
                );
                $tUCMO->update($tData);

                //问题件查询
                $tCHIRow = $tCHIMO->field('*')->where('case_id = ' . $tRow['case_id'] . ' and status = 0')->fRow();

                //插入log
                $tData = array(
                    'title' => '有问题', 
                    'status' => 5,
                    'case_id' => $tRow['case_id'],
                    'created' => time(),
                );
                $tUCLMO->insert($tData);

                //用户案件 问题原因
                $tData = array(
                    'case_id' => $tRow['case_id'], 
                    'datatype' => $tCHIRow['datatype'],
                    'introduce' => $tCHIRow['content'],
                );
                $tUCFMO->insert($tData);



             */
            }elseif($tRow['status'] == 4){#华道撤单
                //用户案件更新
                $tData = array(
                    'is_dishonor' => $tRow['is_dishonor'],    
                    'status' => 3,
                    'dishonor_introduce' => $tRow['dishonor_introduce'],
                    'hd_caseno' => $tRow['hd_caseno'],
                    'loss_notice' => $tRow['loss_notice'],
                    'id' => $tRow['case_id'],

                ); 
                if($tUCMO->update($tData)){
					
					//发送短信提醒
					$tSMO = new SmsModel;
					$tUMO = new UserModel;
					$tSql = 'select u.phone,u.critcode from '.$tUCMO->table.' uc left join '.$tUMO->table.' u on uc.uid = u.id where uc.id = '.$tRow['case_id'];
					$tPhone = $tUCMO->query($tSql)[0]['phone'];
					$tCritcode = $tUCMO->query($tSql)[0]['critcode'];
					if(!empty($tPhone)){
						$tHCTGMO = new Huadao_Core_T_GrouppolicyinsuredModel;
						$tSTMO = new Sms_TemplateModel;
						$tPolicyno = $tHCTGMO->field('POLICYNO')->where('INSUREDIDNO = "'.$tCritcode.'"')->fRow()['POLICYNO'];
						$tMid = $tSTMO->field('tpl_id')->where('policyno = "'.$tPolicyno.'" and type = 3')->fRow()['tpl_id'];
						$tSMO->caseStatus($tPhone,$tMid);
					}
				};
            }

            //更新中间表状态
            $tData = array(
                'status' => 5, 
                'id' => $tRow['id'],
            );
            $tCHMO->update($tData);

        }
    }
}


